import Constants from "../../utils/Constants";
import {getForexList,exportForexList} from "../../services/report/forex";
const R=require("ramda");
export default {
    namespace:"forexReport",
    state:{
        search:{
            lesseeCode:null,
        },
        page:{
        },
        forexList:[]

    },
    reducers: {
        setForexList(state,{payload}) {
            return {...state,forexList:payload.forexList,page:payload.forexPage}
        }
    },
    effects:{
        *getForexList({payload},{call,put,select}){

            payload= R.isNil(payload)?null:payload;
            let page=R.isNil(payload)?Constants.DEFAULTPAGE:R.isNil(payload.page)?Constants.DEFAULTPAGE:payload.page;
            let beginTime=R.isNil(payload)?null:payload.beginTime;
            let endTime=R.isNil(payload)?null:payload.endTime;
            let status=R.isNil(payload)?null:payload.status;
            const res=yield call(getForexList,{beginTime:beginTime,endTime:endTime,status:status,page});
            let content=res.data.content;
            let forexList=R.isNil(content.data.list)?[]:content.data.list;
            if(res.data.resultCode=="ok"){
                yield put({
                    type: 'setForexList',
                    payload: {
                        forexList: forexList,
                        forexPage:R.isNil(content.data.page) ? Constants.DEFAULTPAGE : content.data.page,
                    }
                });
            }
        },

        *exportForexList({payload},{call,put,select}){
            payload= R.isNil(payload)?null:payload;
            let beginTime=R.isNil(payload)?null:payload.beginTime;
            let endTime=R.isNil(payload)?null:payload.endTime;
            let status=R.isNil(payload)?null:payload.status;
            const res=yield call(exportForexList,{beginTime:beginTime,endTime:endTime,status:status});
        }


    },






    subscriptions: {
        setup({history, dispatch}) {
            return history.listen(({pathname}) => {
                if(pathname.includes('report/forex')) {
                    dispatch({
                        type: 'getForexList'
                    });
                }
            });
        }
    }


}